//
//  LoginViewController.m
//  RentCar
//
//  Created by sunmingming on 14-4-1.
//  Copyright (c) 2014年 Ebola. All rights reserved.
//

#import "LoginViewController.h"
#import "Register1ViewController.h"

@interface LoginViewController ()

@property(nonatomic,weak) IBOutlet UIImageView *zyImage;
@property(nonatomic,weak) IBOutlet UILabel *zyLabel;

@property(nonatomic,weak) IBOutlet UITextField *nameField;
@property(nonatomic,weak) IBOutlet UITextField *passField;

@property(nonatomic,weak) IBOutlet UIButton *loginButton;
@property(nonatomic,weak) IBOutlet UIButton *forgetPassButton;

-(IBAction)loginPressed:(id)sender;
-(IBAction)forgetPassPressed:(id)sender;
-(IBAction)directPressed:(id)sender;

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"登录领头洋";
        self.navigationItem.rightBarButtonItem =
        [[UIBarButtonItem alloc] initWithTitle:@"注册"
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(registerPressed:)];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self showError:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.loginButton.enabled = YES;
    if(APPALL.myUserItem.username && (![APPALL.myUserItem.username isEqualToString:@"11111111111"]))
    {
        self.nameField.text = APPALL.myUserItem.username;
        self.passField.text = APPALL.myUserItem.password;
    }
    else
    {
        self.nameField.text = @"";
        self.passField.text = @"";
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//登录请求
-(IBAction)loginPressed:(id)sender
{
    NSString *nameStr = [self.nameField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *passStr = [self.passField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(nameStr.length <= 0)
    {
        [self showError:@"请填写手机号！"];
        return;
    }
    if(passStr.length <= 0)
    {
        [self showError:@"请填写密码！"];
        return;
    }
    if(![tooles validateTelephone:nameStr])
    {
        [self showError:@"手机号码填写不正确！"];
        return;
    }
    [self resignAllTextField];
    [self showError:nil];
    self.loginButton.enabled = NO;
    NSDictionary *parameters=@{@"user":nameStr,@"pass":passStr};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"auth_login" withParameters:parameters block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.loginButton setEnabled:YES];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah....");
                    APPALL.myUserItem = [[SpUserItem alloc] initWithDictionary:aSucData];
                    if(![APPALL.myUserItem.type isEqualToString:@"C"])
                    {
                        [self showError:@"您的身份是理发师，请下载理发师专用客户端！"];
                        [SVProgressHUD dismiss];
                        return;
                    }
                    NSString *passStr = [self.passField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
                    APPALL.myUserItem.password = passStr;
                    [APPALL.myUserItem saveToDB];
                    [APPALL dismissLoginWindow];
                }
                    break;
                default:
                    break;
                    
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resignAllTextField];
}

-(void)resignAllTextField
{
    [self.nameField resignFirstResponder];
    [self.passField resignFirstResponder];
}

-(void)registerPressed:(id)sender
{
    Register1ViewController *rvc = [Register1ViewController new];
    [self.navigationController pushViewController:rvc animated:YES];
}

-(IBAction)forgetPassPressed:(id)sender
{
#if 0
    //忘记密码
    ForgetPWViewController *rvc = [[ForgetPWViewController alloc] init];
    [self.navigationController pushViewController:rvc animated:YES];
#endif
}

-(IBAction)directPressed:(id)sender
{
    NSString *nameStr = @"11111111111";
    NSString *passStr = @"111111";
    [self resignAllTextField];
    [self showError:nil];
    self.loginButton.enabled = NO;
    NSDictionary *parameters=@{@"user":nameStr,@"pass":passStr};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"auth_login" withParameters:parameters block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.loginButton setEnabled:YES];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah....");
                    APPALL.myUserItem = [[SpUserItem alloc] initWithDictionary:aSucData];
                    NSString *passStr = [self.passField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
                    APPALL.myUserItem.password = passStr;
                    NSLog(@"toakenget:%@",APPALL.myUserItem.token);
                    [APPALL dismissLoginWindow];
                }
                    break;
                default:
                    break;
                    
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)showError:(NSString*)aStr
{
    if((!aStr) || aStr.length <= 0)
    {
        self.zyImage.hidden = YES;
        self.zyLabel.hidden = YES;
    }
    else
    {
        self.zyImage.hidden = NO;
        self.zyLabel.hidden = NO;
        self.zyLabel.text = aStr;
    }
}

-(void)dealloc
{
    self.nameField = nil;
    self.passField = nil;
    self.loginButton = nil;
    self.forgetPassButton = nil;
}
@end